package cn.com.shopec.erp.purchase.commo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Component;

import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.purchase.entity.PurchaseOrder;
import cn.com.shopec.erp.purchase.vo.PurchaseOrderVo;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.entity.User;

/**
 * 
 * @ClassName: DefectiveConvertor
 * @Description: 采购对象转换器
 * @author: LiangShunWang
 * @date 2016-11-21 下午17:16:22
 * 
 */
@Component
public class PurchaseVoConvertor {
	
	 public List<PurchaseOrderVo> convert2DefectiveVos(List<PurchaseOrder> purchaseList) {
	        List<PurchaseOrderVo> purchaseVos = null;

	        if (LogicUtil.isNotNullAndEmpty(purchaseList)) {
	        	purchaseVos = new ArrayList<PurchaseOrderVo>();

	            for (PurchaseOrder purchaseOrder : purchaseList) {
	            	PurchaseOrderVo purchaseVo = this.convert2DefectiveVo(purchaseOrder);
	            	purchaseVos.add(purchaseVo);
	            }
	        }

	        return purchaseVos;
	    }

	 public PageFinder<PurchaseOrderVo> convert2PagedDefectiveVos(PageFinder<PurchaseOrder> pagedBrands) {
	        PageFinder<PurchaseOrderVo> pagedBrandVos = null;

	        if (LogicUtil.isNotNull(pagedBrands.getData())) {
	            List<PurchaseOrderVo> defectiveVos = this.convert2DefectiveVos(pagedBrands.getData());

	            pagedBrandVos = new PageFinder<PurchaseOrderVo>(pagedBrands.getPageNo(), pagedBrands.getPageSize(),
	                    pagedBrands.getRowCount(), defectiveVos);
	        }

	        return pagedBrandVos;
	    }
	 
	 public PurchaseOrderVo convert2DefectiveVo(PurchaseOrder purchaseOrder) {
		 PurchaseOrderVo purchaseOrderVo = null;
	        if (LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrderVo = new PurchaseOrderVo();
	        	purchaseOrderVo.setId(purchaseOrder.getId());
	        	purchaseOrderVo.setPoNo(purchaseOrder.getPoNo());
//	        	purchaseOrderVo.setPrNo(purchaseOrder.getPrNo());
	        	purchaseOrderVo.setFoPurDate(purchaseOrder.getFoPurDate());
	        	purchaseOrderVo.setAcPurDate(purchaseOrder.getAcPurDate());
	        	purchaseOrderVo.setPoStatus(purchaseOrder.getPoStatus());
	        	purchaseOrderVo.setCurAuditPoint(purchaseOrder.getCurAuditPoint());
	        	purchaseOrderVo.setLastAuditRole(purchaseOrder.getLastAuditRole());
	        	purchaseOrderVo.setCreatePerson(purchaseOrder.getCreatePerson());
	        	purchaseOrderVo.setCreateTime(purchaseOrder.getCreateTime());
	        	purchaseOrderVo.setLastAuditPerson(purchaseOrder.getLastAuditPerson());
	        	purchaseOrderVo.setLastAuditTime(purchaseOrder.getLastAuditTime());
	        	purchaseOrderVo.setDeleteFlag(purchaseOrder.getDeleteFlag());
	        	purchaseOrderVo.setPurcType(purchaseOrder.getPurcType());
	        	purchaseOrderVo.setOverData(purchaseOrder.getOverData());
	        	purchaseOrderVo.setPoName(purchaseOrder.getPoName());
	        	purchaseOrderVo.setAuditResult(purchaseOrder.getAuditResult());
	        	purchaseOrderVo.setRemark(purchaseOrder.getRemark());
	        	purchaseOrderVo.setAuditTime(purchaseOrder.getAuditTime());
	        	purchaseOrderVo.setSupName(purchaseOrder.getSupName());
	        	purchaseOrderVo.setSupCode(purchaseOrder.getSupCode());
	        	purchaseOrderVo.setPrCreaterDate(purchaseOrder.getPrCreaterDate());
	        	purchaseOrderVo.setReturnQuantity(purchaseOrder.getReturnQuantity());
	        	purchaseOrderVo.setPpNumber(purchaseOrder.getPpNumber());
	        	purchaseOrderVo.setPpUnitPrice(purchaseOrder.getPpUnitPrice());
	        	purchaseOrderVo.setTotalPrice(purchaseOrder.getTotalPrice());
	        	purchaseOrderVo.setPpUnitPrice(purchaseOrder.getPpUnitPrice());
	        }
	        return purchaseOrderVo;
	    }
	 
	 public PurchaseOrder convert2Defective(PurchaseOrderVo purchaseOrderVo) {
		 PurchaseOrder purchaseOrder = null;

	        if (LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrder = new PurchaseOrder();
	        	purchaseOrder.setId(purchaseOrderVo.getId());
	        	purchaseOrder.setPoNo(purchaseOrderVo.getPoNo());
//	        	purchaseOrder.setPrNo(purchaseOrderVo.getPrNo());
	        	purchaseOrder.setFoPurDate(purchaseOrderVo.getFoPurDate());
	        	purchaseOrder.setAcPurDate(purchaseOrderVo.getAcPurDate());
	        	purchaseOrder.setPoStatus(purchaseOrderVo.getPoStatus());
	        	purchaseOrder.setCurAuditPoint(purchaseOrderVo.getCurAuditPoint());
	        	purchaseOrder.setLastAuditRole(purchaseOrderVo.getLastAuditRole());
	        	purchaseOrder.setCreatePerson(purchaseOrderVo.getCreatePerson());
	        	purchaseOrder.setCreateTime(purchaseOrderVo.getCreateTime());
	        	purchaseOrder.setLastAuditPerson(purchaseOrderVo.getLastAuditPerson());
	        	purchaseOrder.setLastAuditTime(purchaseOrderVo.getLastAuditTime());
	        	purchaseOrder.setDeleteFlag(purchaseOrderVo.getDeleteFlag());
	        	purchaseOrder.setPurcType(purchaseOrderVo.getPurcType());
	        	purchaseOrder.setOverData(purchaseOrderVo.getOverData());
	        	purchaseOrder.setPoName(purchaseOrderVo.getPoName());
	        	purchaseOrder.setAuditResult(purchaseOrderVo.getAuditResult());
	        	purchaseOrder.setRemark(purchaseOrderVo.getRemark());
	        	purchaseOrder.setAuditTime(purchaseOrderVo.getAuditTime());
	        	purchaseOrder.setSupName(purchaseOrderVo.getSupName());
	        	purchaseOrder.setSupCode(purchaseOrderVo.getSupCode());
	        	purchaseOrder.setPrCreaterDate(purchaseOrderVo.getPrCreaterDate());
	            if (purchaseOrderVo.getDeleteFlag() == null ) {
	            	purchaseOrder.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
	            } else {
	            	purchaseOrder.setDeleteFlag(purchaseOrderVo.getDeleteFlag());
	            }
	        }

	        return purchaseOrder;
	    }
	 
	 public PurchaseOrder convert2Defective(PurchaseOrderVo purchaseOrderVo, PurchaseOrder purchaseOrder) {

	        if (LogicUtil.isNotNull(purchaseOrderVo) && LogicUtil.isNotNull(purchaseOrder)) {
//	        	purchaseOrder.setCode();
	        	purchaseOrder.setAuditTime(DateUtil.formatTimesTampDate(DateUtil.getCurrentDate()));
	        	purchaseOrder.setLastAuditPerson(UserUtils.getLoginUserName());
	            return purchaseOrder;
	        } else {
	            return null;
	        }
	    }
	
	 public PurchaseOrder convert2DefectiveUpdateAuditResult(String purchaseNo, PurchaseOrder purchaseOrder,int status) {
		 
	        if (LogicUtil.isNotNull(purchaseNo) && LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrder.setPoStatus(status);
	        	if(status==5){
	        		purchaseOrder.setCurAuditPoint(ProductSCMConstant.PURCHASERETURN_CURAUDIT_SUCCESS);
	        	}
	        	purchaseOrder.setPrCreaterDate(DateUtil.formatTimesTampDate(DateUtil.getCurrentDate()));
	            return purchaseOrder;
	        } else {
	            return null;
	        }
	    }
	 
	 /**
	  * 改变采购单的状态为：采购中
	  * 
	  */
	 public PurchaseOrder convert2DefectiveList(String poNo, PurchaseOrder purchaseOrder) {

	        if (LogicUtil.isNotNull(purchaseOrder)) {
	        	purchaseOrder.setAuditResult(ProductSCMConstant.LASTAUDITRESULT_OK);
	        	purchaseOrder.setPoStatus(ProductSCMConstant.PO_STATUS_THREE);
	            return purchaseOrder;
	        } else {
	            return null;
	        }
	    }
}
